<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="用户名称">
          <el-input
            v-model="form.nickname"
            placeholder="请输入用户名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户编号">
          <el-input
            v-model="form.user_id"
            placeholder="请输入用户编号"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="工种" class="movement">
          <el-select v-model="form.names" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提成方式">
          <el-select v-model="form.type" placeholder="请选择提成方式">
            <el-option
              v-for="item in options1"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="form.time"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            @change="timeChange"
          >
          </el-date-picker>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <span class="gauge">返现总计金额: {{ count_price }}元</span>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        border
        style="width: 100%"
        max-height="600"
      >
        <el-table-column
          label="序号"
          type="index"
          width="70"
          :index="indexMethod"
          align="center"
        >
        </el-table-column>
        <!-- <el-table-column prop="id" align="center" label="ID" width="70">
        </el-table-column> -->
        <el-table-column prop="nickname" align="center" label="分享人">
        </el-table-column>
        <el-table-column prop="id" align="center" label="分享人编号">
        </el-table-column>
        <el-table-column prop="fx_renshus" align="center" label="分享人数">
          <!-- <template slot-scope="scope">
            <span>{{
              scope.row.user_authority == 3 ? scope.row.work_name : "勘察员"
            }}</span>
          </template> -->
        </el-table-column>
        <el-table-column prop="fanli" align="center" label="分享消费总金额">
        </el-table-column>
        <!-- <el-table-column
          prop="user_authority_proportion_type"
          align="center"
          label="提成方式"
        >
          <template slot-scope="scope">
            <span>{{
              scope.row.user_authority_proportion_type == 1
                ? "材料费+工时费"
                : scope.row.user_authority_proportion_type == 2
                ? "工时费"
                : ""
            }}</span>
          </template>
        </el-table-column> -->
        <!-- <el-table-column
          prop="user_authority_proportion"
          align="center"
          label="返现比例"
        >
        </el-table-column> -->
        <!-- <el-table-column
          prop="single_quantity"
          align="center"
          label="完成订单数"
        >
        </el-table-column>
        <el-table-column prop="performance" align="center" label="绩效">
        </el-table-column>
        <el-table-column prop="ticheng" align="center" label="提成金额（总）">
        </el-table-column>
        <el-table-column align="center" label="罚款金额" width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.worker_examines_length }}</span>
          </template>
        </el-table-column> -->

        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              size="mini"
              type="primary"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 150, 200, 300]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 详情 -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="70%"
      :before-close="handleClose"
    >
      <el-table :data="details" border style="width: 100%" max-height="600">
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="70"
          :index="Method"
        >
        </el-table-column>
        <el-table-column prop="nickname" align="center" label="消费用户名">
        </el-table-column>
        <el-table-column prop="user_ids" align="center" label="消费用户编号">
        </el-table-column>
        <!-- <el-table-column
          prop="times"
          align="center"
          label="订单金额（工时费+材料费）"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.order_length }}</span>
          </template>
        </el-table-column> -->
        <!-- <el-table-column prop="times" align="center" label="提成方式">
        </el-table-column>
        <el-table-column prop="order_id" align="center" label="提成比例">
        </el-table-column> -->
        <el-table-column prop="payed" align="center" label="消费金额">
        </el-table-column>
        <el-table-column prop="time" align="center" label="统计时间">
        </el-table-column>
        <!-- <el-table-column
          prop="performance"
          align="center"
          label="绩效（工时费）"
        >
        </el-table-column> -->
        <!-- <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button @click="dialClick(scope.row)" size="mini" type="primary"
              >查看</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"
          :current-page="page1"
          :page-sizes="[10, 150, 200, 300]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total1"
        >
        </el-pagination>
      </div>
      <!-- <el-dialog width="30%" :visible.sync="innerVisible" append-to-body>
        <el-table :data="itemsUser" border style="width: 100%" max-height="600">
          <el-table-column prop="name" align="center" label="商品名称">
          </el-table-column>
          <el-table-column prop="nums" align="center" label="商品数量">
          </el-table-column>
          <el-table-column prop="price" align="center" label="商品价格">
          </el-table-column>
        </el-table>
      </el-dialog> -->
    </el-dialog>
  </el-card>
</template>

<script>
import { workList, particular } from "../../../utils/api.js";
import {
  getCashbackStatistics,
  getCashbackStatisticsDetail,
} from "../../../utils/main";
export default {
  data() {
    return {
      //弹窗
      dialogVisible: false,
      innerVisible: false,
      token: sessionStorage.getItem("token"),
      //请求特效
      loading: false,
      //查询参数
      form: {
        user_id: "",
        nickname: "",
      },
      options: [
        // {
        //   value: "kanchayuan",
        //   label: "勘察员",
        // },
      ],
      options1: [
        {
          id: "1",
          name: "材料费+工时费",
        },
        {
          id: "2",
          name: "工时费",
        },
      ],
      count_price: "",
      //展示数据
      tableData: [
        {
          user_id: "",
          name: "",
          time: "",
          mobile: "",
          single_quantity: "",
          ticheng: "",
        },
      ],
      //详情展示数据
      details: [
        {
          mobile: "",
          name: "",
          times: "",
          order_id: "",
          items: [{}],
        },
      ],
      //具体数据
      itemsUser: {
        name: "",
        nums: "",
        price: "",
      },
      userId: "",
      // 分页器
      page: 1,
      limit: 10,
      total: 0,
      names: "",
      type: "",
      total1: 0,
      limit1: 10,
      page1: 1,
      authority_id: "3,4",
      name: "",
      mobile: "",
      name: "",
      type: "",
      time: "",
    };
  },
  methods: {
    //查询
    onSubmit() {
      // if (this.form.time == null) {
      //   this.form.time = "";
      // }
      // this.name = this.form.name;
      // this.mobile = this.form.mobile;
      // this.names = this.form.names;
      // this.type = this.form.type;
      // this.time = this.form.time;
      this.page = 1;
      // console.log(this.form);
      this.workLists();
    },
    // 重置
    onReset() {
      (this.form = {
        user_id: "",
        nickname: "",
      }),
        (this.page = 1);
      this.workLists();
    },
    //分页器
    handleSizeChange(val) {
      this.limit = val;
      this.workLists();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.workLists();
    },
    handleSizeChange1(val) {
      this.limit1 = val;
      this.particulars();
    },
    handleCurrentChange1(val) {
      this.page1 = val;
      this.particulars();
    },
    //获取工种下拉框
    // async getSelect() {
    //   // this.loading = true;
    //   let res = await getGongZhongSelect(this.token, 1)
    //     .then((res) => {
    //       if (res.data.status) {
    //         this.options = res.data.data;
    //         this.options.unshift({ id: "kanchayuan", name: "勘察员" });
    //         // this.total = res.data.data.count;
    //         // console.log(111, this.options);
    //         // let timer = setTimeout(() => {
    //         //   this.loading = false;
    //         //   clearTimeout(timer);
    //         // }, 400);
    //       } else {
    //         if (res.data.msg == "token失效,请重新进行登录") {
    //           this.$router.push("/login");
    //         }
    //         if (res.data.msg == "此账号没有权限") {
    //           this.$message.error(res.data.msg);
    //           this.loading = false;
    //         }
    //         // let timer = setTimeout(() => {
    //         //   this.$message.error(res.data.msg);
    //         // }, 5000);
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err.message);
    //     });
    // },
    //日期
    timeChange(e) {
      // let date = new Date(e);
      // // console.log(day);
      // let year = date.getFullYear();
      // let month = date.getMonth() + 1;
      // let day = date.getDate();
      // this.form.time = year + "-" + month + "-" + day;
      // console.log(this.form.time);
      this.time = this.form.time;
      this.workLists;
    },
    //获取列表数据
    workLists() {
      this.loading = true;

      // console.log(this.form.time);
      // return;
      getCashbackStatistics(
        this.token,
        this.form.nickname,
        this.form.user_id,
        this.page,
        this.limit
      )
        .then((res) => {
          if (res.data.status) {
            this.tableData = res.data.data.data;
            // this.tableData.single_quantity =
            //   this.tableData[0].worker_examines[0].single_quantity;
            // console.log(this.tableData.single_quantity);
            this.count_price = res.data.data.sum;
            this.total = res.data.data.count;
            // this.tableData.forEach((item) => {
            //   if (item.worker_examines.length == 0) {
            //     item.worker_examines_length = "0:00";
            //   } else {
            //     item.worker_examines_length = item.worker_examines[0].substract;
            //   }
            //   // console.log(item);
            // });

            let timer = setTimeout(() => {
              this.loading = false;
              clearTimeout(timer);
            }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //获取详情列表数据
    particulars() {
      // this.loading = true;
      // if (this.form.time == "") {
      //   this.tableData.time = "";
      // }
      // console.log(111, this.tableData.time);
      getCashbackStatisticsDetail(
        this.token,
        this.userId,
        this.page1,
        this.limit1
      )
        .then((res) => {
          if (res.data.status) {
            this.details = res.data.data.data;
            this.total1 = res.data.data.count;
            this.details.forEach((item) => {
              if (item.order_id == null) {
                item.order_id = "暂无数据";
              }
              if (item.order.length == 0) {
                item.order_length = "";
              } else {
                item.order_length = item.order[0].order_amount;
                console.log(item.order_length);
              }
            });
            let timer = setTimeout(() => {
              this.loading = false;
              clearTimeout(timer);
            }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //点击查看
    handleClick(row) {
      let obj = JSON.parse(JSON.stringify(row));
      // this.tableData.time = row.time;
      // this.tableData.user_id = row.id;
      this.userId = obj.id;
      (this.page1 = 1), (this.limit1 = 10), this.particulars();
      this.dialogVisible = true;
    },
    // dialClick(row) {
    //   let obj = JSON.parse(JSON.stringify(row));
    //   this.itemsUser = obj.items;
    //   this.innerVisible = true;
    // },
    // 序号排序
    indexMethod(index) {
      if (this.total < this.limit) {
        return this.total - index;
      } else {
        return this.total - (this.page - 1) * this.limit - index;
      }
    },
    Method(index) {
      if (this.total1 < this.limit1) {
        return this.total1 - index;
      } else {
        return this.total1 - (this.page1 - 1) * this.limit1 - index;
      }
    },
    //详情关闭
    handleClose(done) {
      done();
      this.workLists();
    },
  },
  mounted() {
    this.workLists();
    // this.getSelect();
  },
};
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.pageUser {
  margin-top: 3%;
}
.gauge {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
  margin-right: 20px;
  font-size: 18px;
  color: rgba(249, 100, 34, 1);
}
</style>